﻿Imports ecomLibrary, utilitiesLibrary

Public Class store
    Inherits System.Web.UI.Page
    Dim userID
    Dim shopCart As cart
    Dim objStores As New Stores
    Dim pxy As Object
    Dim deptDS As DataSet
    Dim prodDS As DataSet

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        userID = Session("login")
        shopCart = Session("cart")


        If userID Is Nothing Then
            Response.Redirect("http://cis-iis2.temple.edu/Fall2011/434202/TermProjectCL/Login/storefront.aspx")
            Exit Sub
        End If

        If Not IsPostBack Then
            displayUsername()
        End If

    End Sub


    Protected Sub ddlSelDept_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlSelDept.SelectedIndexChanged
        Dim prodDS As DataSet

        prodDS = Session("pxy").getProductCatalog(ddlSelDept.SelectedValue)
        Session("prodDS") = prodDS
        prodDS.Tables(0).Columns(1).ColumnName = "Desc"
        prodDS.Tables(0).Columns(2).ColumnName = "Price"
        prodDS.Tables(0).Columns(3).ColumnName = "URL"
        gvProducts.DataSource = prodDS
        gvProducts.DataBind()

        btnAddCart.Visible = True

        'Session("cart") = shopCart
    End Sub

    Protected Sub ddlStore_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlStore.SelectedIndexChanged

        If ddlStore.SelectedValue = 0 Then
            Exit Sub
        End If

        Session("store") = ddlStore.SelectedValue
        pxy = objStores.getPxy(Session("store"))
        Session("pxy") = pxy
        deptDS = pxy.getDepartments

        ddlSelDept.DataSource = deptDS
        ddlSelDept.DataValueField = deptDS.Tables(0).Columns(0).ColumnName
        ddlSelDept.DataTextField = deptDS.Tables(0).Columns(1).ColumnName
        ddlSelDept.DataBind()

        ddlSelDept.Visible = True
        lblSelectDept.Visible = True

        'Session("cart") = shopCart
    End Sub

    Protected Sub btnAddCart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddCart.Click
        Dim Tbox As New TextBox
        Dim qtyOrdered As Integer
        Dim objProd As New product
        Dim prodNum As Integer
        Dim storeID As Integer = Session("store")
        Dim prodDS As DataSet = Session("prodDS")
        Dim url As String
        Dim desc As String
        Dim price As Decimal
        Dim qtyAdded As Integer = 0


        For I As Integer = 0 To gvProducts.Rows.Count - 1
            Tbox = CType(gvProducts.Rows(I).FindControl("txtQuantity"), TextBox)


            Try
                objProd = New product
                qtyOrdered = CType(Tbox.Text, Integer)
                If Not IsNumeric(qtyOrdered) Then
                    lblInfo.Text = "Only enter Integers into the quantity boxes."
                    Exit Sub
                End If

                prodNum = CType(prodDS.Tables(0).Rows(I).Item(0), Integer)
                url = prodDS.Tables(0).Rows(I).Item(3).ToString
                desc = prodDS.Tables(0).Rows(I).Item(1).ToString
                price = CType(gvProducts.Rows(I).Cells(1).Text, Decimal)


                objProd.ProdNo = prodNum
                objProd.Description = desc
                objProd.quantityOrd = qtyOrdered
                objProd.Price = price
                objProd.storeID = storeID
                objProd.url = url
                objProd.total = CType(objProd.Price * objProd.quantityOrd, Decimal)

                If shopCart.cart.Contains(objProd) Then
                    shopCart.changeQuantOrdered(objProd, objProd.quantityOrd)
                    qtyAdded += 1
                Else
                    shopCart.AddProduct(objProd)
                    qtyAdded += 1
                    Tbox.Text = ""
                End If

            Catch ex As Exception
            End Try

        Next

        If qtyAdded > 0 Then
            lblInfo.Visible = True
            lblInfo.Text = qtyAdded.ToString & " items have been added to your cart. Select Continue to view your cart and/or checkout."
            btnContinue.Visible = True
        End If



    End Sub

    Protected Sub btnContinue_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnContinue.Click
        'shopCart.emptyCart()
        Session("cart") = shopCart
        Response.Redirect("../Shopping/checkout.aspx")
    End Sub

    Protected Sub displayUsername()
        Dim userSplit As Array
        userSplit = userID.Split("@")
        userID = userSplit(0)
        userID = userID & "!"
        lblUser.Text = userID
    End Sub

    Protected Sub btnCart_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCart.Click
        Session("cart") = shopCart
        Response.Redirect("checkout.aspx")
    End Sub

    Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogout.Click
        Dim objSer As New Serialize
        Dim objCart As cart = Session("cart")
        Dim LoginID As String = objCart.LoginID

        objSer.WriteCartS(objCart, LoginID)

        Response.Redirect("../Login/storefront.aspx")
    End Sub
End Class